<?php
class StringFormat {

    const USD_FORMAT = "USD";

    public function formatCurrency($price,$currency = StringFormat::USD_FORMAT){
        if($currency == StringFormat::USD_FORMAT && is_numeric($price)){
             $priceLen = strlen($price);
             $percent = $priceLen > 1 ? $price[$priceLen-2].$price[$priceLen-1] : "0";
             $price =   substr($price, 0, $priceLen-2);
             while (true) {
                $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $price);
                if ($replaced != $price) {
                    $price = $replaced;
                } else {
                    break;
                }
             }
             return "$".($price!=""?$price:"0").($percent!= "00" && $percent!=""?".$percent":"");

        }
        return "$0";
    }

    public function formatCurrencyBasic($price,$currency = StringFormat::USD_FORMAT){
        $price=(string)$price;
        if($currency == StringFormat::USD_FORMAT && is_numeric($price)){
             $priceLen = strlen($price);
             $percent = $priceLen > 1 ? $price[$priceLen-2].$price[$priceLen-1] : "0";
             $price =   substr($price, 0, $priceLen-2);
             return ($price!=""?$price:"0").($percent!= "00" && $percent!=""?".$percent":"");
        }
        return "0";
    }

}
?>
